package org.jbit.dao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class BaseDao extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Connection conn;
	private static String Url;
	private static String DatabaseName;
	private static String User;
	private static String password;
	
	public static Connection getConnection() throws IOException {
		DomParse();
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			String UrlAll="jdbc:sqlserver://"+Url+";DatabaseName="+DatabaseName;
			conn = DriverManager.getConnection(UrlAll,User,password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void closeAll(ResultSet rs, PreparedStatement ps,
			Connection conn) {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}

			if (ps != null) {
				ps.close();
				ps = null;
			}
			if (conn != null && !conn.isClosed()) {
				conn.close();
			}

		} catch (SQLException ex1) {
			System.err.println("" + ex1.getMessage());
		}
	}

	
	public static void closeAll(ResultSet rs, Statement stm, Connection conn) {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}

			if (stm != null) {
				stm.close();
				stm = null;
			}
			if (conn != null && !conn.isClosed()) {
				conn.close();
			}

		} catch (SQLException ex1) {
			System.err.println(" " + ex1.getMessage());
		}
	}
	public void init() throws ServletException {
		// Put your code here
	}
	
	public static void DomParse(){
		DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder dombuilder=domfac.newDocumentBuilder();
			InputStream is=new FileInputStream("/dataBase.xml");
			Document doc=dombuilder.parse(is);
			Element root=doc.getDocumentElement();//获取文档结点
			NodeList Context=root.getChildNodes();//获取根结点
			
			if(Context!=null){
				for(int i=0;i<Context.getLength();i++){
					Node DataBase=Context.item(i);
					if(DataBase.getNodeType()==Node.ELEMENT_NODE){
						for(Node node=DataBase.getFirstChild();node!=null;node=node.getNextSibling()){
							if(node.getNodeType()==Node.ELEMENT_NODE){
								if(node.getNodeName().equals("Url")){
									Url=node.getFirstChild().getNodeValue();
									}
								if(node.getNodeName().equals("DatabaseName")){
									DatabaseName=node.getFirstChild().getNodeValue();
									}
								if(node.getNodeName().equals("User")){
									User=node.getFirstChild().getNodeValue();
									}
								if(node.getNodeName().equals("password")){
									password=node.getFirstChild().getNodeValue();
									}
								}
							}
						}
					}
				}
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
					} catch (SAXException e) {
						e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
							}
		}
}
